const { Sequelize, Model, INTEGER } = require('sequelize')
const bcryptjs = require('bcryptjs')
const { db } = require('../core/sequelize')
const { NotFound, AuthFailed } = require('../core/http-exception')
class FollowUser extends Model {
  //添加
  static async findOrCreateFollowUser(info) {
    const { userid, followuserid } = info
    const [user, created] = await FollowUser.findOrCreate({
      where: {
        userid, followuserid
      },
      defaults: info
    })
    return created

  }
  static async getDetails(followuserid, userid) {
    const follow = await FollowUser.findOne({
      where: {
        userid,
        followuserid
      }
    })
    return follow
  }
  //查询是否存在
  static async getFollowUserId(followuserid, userid) {
    const followUser = await FollowUser.findOne({
      where: {
        followuserid, userid,
      },
    })
    return followUser
  }
}
FollowUser.init(
  {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true, //设置主键
      autoIncrement: true,
    },
    userid: Sequelize.INTEGER,
    followuserid: Sequelize.INTEGER, //类型 字符串
  },
  {
    sequelize: db,
    modelName: 'followUser',
    timestamps: false
  }
)
module.exports = { FollowUser }
